package com.heima.admin.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 频道相关的的controller
 */
@Api(tags = "频道管理",description = "频道管理的接口")
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {

    @Autowired
    private AdChannelService adChannelService;

    /**
     * 分页查询频道信息
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询频道")
    @ApiImplicitParam(name = "dto", value = "频道请求对象", required = true, dataType = "ChannelDto")
    public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto dto){
        return adChannelService.findByPage(dto);
    }

    /**
     * 新增频道列表
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增频道")
    @ApiImplicitParam(name = "channelDto", value = "新增频道的信息", required = true, dataType = "ChannelDto")
    public ResponseResult save(@RequestBody ChannelDto channelDto){
        if(channelDto==null || StringUtils.isBlank(channelDto.getName())){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);//缺少参数
        }
        //判断频道名是否存在
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdChannel::getName,channelDto.getName());
        AdChannel one = adChannelService.getOne(wrapper);
        if(one!=null){
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST);
        }
        //转换对象
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        adChannel.setCreatedTime(new Date());//设置创建时间
        //添加
        boolean save = adChannelService.save(adChannel);
        if(!save){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

    /**
     *  频道删除
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除频道")
    @ApiImplicitParam(name = "id", value = "要删除的频道id", required = true, dataType = "Long")
    public ResponseResult delete(@PathVariable("id") Long id){
        if (id==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        adChannelService.deleteById(id);
        return ResponseResult.ok();
    }

    /**
     * 修改频道信息
     * 修改频道状态
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改频道")
    @ApiImplicitParam(name = "channelDto", value = "修改频道的信息", required = true, dataType = "ChannelDto")
    public ResponseResult update(@RequestBody ChannelDto channelDto){
        if(channelDto==null || channelDto.getId()==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);//缺少参数
        }
        //判断频道名是否存在
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        //排除自己
        wrapper.eq(AdChannel::getName,channelDto.getName()).ne(AdChannel::getId,channelDto.getId());
        AdChannel one = adChannelService.getOne(wrapper);
        if(one!=null){
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST);
        }
        //转换对象
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        boolean update = adChannelService.updateById(adChannel);
        if(!update){
            throw new LeadException(AppHttpCodeEnum.UPDATE_ERROR);
        }
        return ResponseResult.ok();
    }























}
